package v1

import (
	"fmt"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
	"repairs/pkg/app"
	"repairs/pkg/e"
	"repairs/pkg/setting"
	"repairs/pkg/util"
	"repairs/service/room_service"
)

func Rooms(c *gin.Context) {
	method := c.Query("_method")
	print("GET ",method)
	if method == "get" {
		GetRooms(c)
	} else if method == "add" {
		AddRoom(c)
	}
}

type GetRoomJSON struct {
	PageNum int `json:"page_num" valid:"Required"`
}

// @Summary 获取教室列表
// @Accept json
// @Tags 教室
// @Produce json
// @Param page_num body int true "页号"
// @Success 200 {object} app.Response
// @Success 500 {object} app.Response
// @Router /api/v1/rooms [get]
func GetRooms(c *gin.Context) {
	var (
		appG                        = app.Gin{C: c}
		data map[string]interface{} = nil
		json GetRoomJSON
	)
	httpCode, errCode := app.BindAndValid(c, &json)
	for {
		if errCode != e.SUCCESS {
			break
		}

		roomService := room_service.RoomRecord{
			PageNum:  util.GetPageByNum(json.PageNum),
			PageSize: setting.AppSetting.PageSize,
		}
		total, err := roomService.Count()
		if err != nil {
			httpCode, errCode = http.StatusInternalServerError, e.ErrorCountRoomFail
			break
		}

		rooms, err := roomService.GetAll()
		if err != nil {
			httpCode, errCode = http.StatusInternalServerError, e.ErrorGetRoomFail
			break
		}
		data = make(map[string]interface{})
		data["lists"] = rooms
		data["total"] = total
		break
	}
	fmt.Printf("DEBUG %#v", data)
	appG.Response(httpCode, errCode, data)
}

// @Summary 删除教室
// @Accept json
// @Tags 教室
// @Param id path int true "教室id"
// @Success 200 {object} app.Response
// @Success 500 {object} app.Response
// @Router /api/v1/rooms/{id} [delete]
func DeleteRoom(c *gin.Context) {
	var (
		appG              = app.Gin{C: c}
		valid             = validation.Validation{}
		id                = com.StrTo(c.Param("id")).MustInt()
		httpCode, errCode = http.StatusOK, e.SUCCESS
	)
	valid.Min(id, 1, "id").Message("ID必须大于0")
	for {
		if valid.HasErrors() {
			app.MarkErrors(valid.Errors)
			httpCode, errCode = http.StatusOK, e.InvalidParams
			break
		}

		roomService := room_service.RoomRecord{ID: id}

		exists, err := roomService.ExistById()
		if err != nil {
			httpCode, errCode = http.StatusInternalServerError, e.ERROR
		}
		if !exists {
			httpCode, errCode = http.StatusOK, e.ErrorNotExistRoomFail
			break
		}
		err = roomService.Delete()
		if err != nil {
			httpCode, errCode = http.StatusInternalServerError, e.ErrorDeleteRoomFail
			break
		}
		break
	}
	appG.Response(httpCode, errCode, nil)
}

type AddRoomJSON struct {
	Room string `json:"room" valid:"Required;MaxSize(20)"`
}

// @Summary 添加教室
// @Accept json
// @Tags 教室
// @Produce json
// @Param room body string true "教室"
// @Success 200 {object} app.Response
// @Success 500 {object} app.Response
// @Router /api/v1/rooms [post]
func AddRoom(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		json AddRoomJSON
	)
	httpCode, errCode := app.BindAndValid(c, &json)
	for {
		if errCode != e.SUCCESS {
			break
		}

		roomService := room_service.RoomRecord{Room: json.Room}
		exist, err := roomService.ExistByRoom()
		if err != nil {
			httpCode, errCode = http.StatusInternalServerError, e.ErrorSystemDataBaseFail
			break
		}
		if exist {
			httpCode, errCode = http.StatusBadRequest, e.ErrorRoomExistFail
			break
		}
		if err := roomService.Add(); err != nil {
			httpCode, errCode = http.StatusInternalServerError, e.ErrorAddRoomFail
			break
		}
		break
	}
	appG.Response(httpCode, errCode, nil)
}
